package com.finance.system.controller;

import com.finance.base.constant.BaseConstants;
import com.finance.base.enums.StateEnum;
import com.finance.base.exception.BaseException;
import com.finance.framework.controller.BaseController;
import com.finance.framework.controller.Result;
import com.finance.framework.lang.BeanUtils;
import com.finance.framework.lang.Page;
import com.finance.framework.lang.PageResultBO;
import com.finance.system.bean.pojo.SysConfig;
import com.finance.system.bean.pojo.SysConfigList;
import com.finance.system.bean.pojo.User;
import com.finance.system.request.SysConfigListReq;
import com.finance.system.request.SysConfigReq;
import com.finance.system.service.IConfigService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;


/**
 * Created by CBWL on 2017/3/15.
 */
@Controller
@RequestMapping("/system/sysconfig")
public class SysConfigController extends BaseController{
    @Resource
    private IConfigService configService;

    @RequestMapping("/index.htm")
    public String index(){
        return "system/sysconfig/index";
    }

    /**
     * 系统配置信息
     * @return
     */
    @RequestMapping("/list")
    public Result list(SysConfigListReq form){
        Page page = Page.getInstance(form);
        SysConfigList sysconfigList = new SysConfigList();
        try {
            BeanUtils.copyProperties(sysconfigList,form);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            throw new BaseException(BaseConstants.ERR_COPYBEAN);
        }
        page.setParams(sysconfigList);
        PageResultBO<SysConfig> resultBO = this.configService.listByPage(page);
        return getSuccessResult(resultBO);
    }

    @RequestMapping("/add.htm")
    public String add(){
        return "system/sysconfig/add";
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@Valid SysConfigReq form){
        User authUser = getAuthUser();//获取当前操作人
        SysConfig sysConfig = new SysConfig();
        if(this.configService.findConfig(form.getConfigName()) != null){
            throw new BaseException(BaseConstants.ERR_DUPLICATE_DATA);
        }
        try {
            BeanUtils.copyProperties(sysConfig,form);
            if(authUser != null){
                sysConfig.setCreateUid(authUser.getId());
                sysConfig.setState(StateEnum.Valid.getState());
                this.configService.addConfig(sysConfig.getConfigName(),sysConfig.getConfigValue());
            }
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            this.logger.error(e.getMessage());
            return getFailResult(BaseConstants.ERR_UNKOWN,"新增失败");
        }
       return getSuccessResult("新增配置信息成功");
    }

    @RequestMapping(value = "/update.htm",method = RequestMethod.GET)
    public String update(String id,HttpServletRequest request){
        SysConfig sysConfig = this.configService.getById(Long.parseLong(id));
        request.setAttribute("sysConfig",sysConfig);
        return "system/sysconfig/update";
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result update(@Valid SysConfigReq sysconfigReq){
        SysConfig oldSysConfig = this.configService.getById(Long.parseLong(sysconfigReq.getId()));
        if(oldSysConfig == null){
            throw new BaseException(BaseConstants.ERR_NON_EXISTENT);
        }
        User authUser = getAuthUser();//获取当前操作人
        SysConfig sysConfig = new SysConfig();
        try {
            BeanUtils.copyProperties(sysConfig,sysconfigReq);
        } catch (IllegalAccessException e) {
            logger.error(e.getMessage(),e);
        } catch (InvocationTargetException e) {
            logger.error(e.getMessage(),e);
        }
        sysConfig.setUpdateUid(authUser.getId());
        try {
            this.configService.update(sysConfig);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            return getFailResult(BaseConstants.ERR_UNKOWN,"更新失败");
        }
        return getSuccessResult("更新配置信息成功");
    }

    @RequestMapping("/delete")
    public Result delete(String key) throws BaseException{
        try {
            this.configService.removeConfig(key);
        } catch (BaseException e) {
            logger.error(e.getMessage(),e);
            logger.error(e.getMessage());
            return getFailResult(BaseConstants.ERR_UNKOWN,"删除失败");
        }
        return getSuccessResult("删除配置信息成功");
    }
}
